package android.slc.appbase.repository.local;

import android.slc.appdb.core.DaoMaster;
import android.slc.appdb.sys.entity.CsState;
import android.slc.appdb.sys.entity.CsState_;
import android.slc.appdb.sys.service.CsStateDaoService;

import io.objectbox.query.QueryBuilder;

/**
 * @author slc
 * @date 2020-09-03 17:03
 */
public class StateDaoServiceDelegate {
    private static final CsStateDaoService CS_STATE_DAO_SERVICE = DaoMaster.getService(CsStateDaoService.class);

    public static CsState getState(String hostName, String hostId) {
        return CS_STATE_DAO_SERVICE.getBox().query().equal(CsState_.hostName, hostName, QueryBuilder.StringOrder.CASE_SENSITIVE).and().equal(CsState_.hostId, hostId, QueryBuilder.StringOrder.CASE_SENSITIVE).build().findUnique();
    }

    public static CsState getStateNotNull(String hostName, String hostId) {
        CsState csState = getState(hostName, hostId);
        if (csState == null) {
            csState = putState(hostName, hostId, 0);
        }
        return csState;
    }

    public static CsState putState(Class classValue, String hostId, int stateValue) {
        return putState(classValue.getName(), hostId, stateValue);
    }

    public static CsState putState(String hostName, String hostId, int stateValue) {
        CsState csState = getState(hostName, hostId);
        if (csState == null) {
            csState = new CsState();
            csState.setHostName(hostName);
            csState.setHostId(hostId);
        }
        csState.set_status(stateValue);
        CS_STATE_DAO_SERVICE.put(csState);
        return csState;
    }
}
